package com.example.cinemaorder.controller;

import com.example.cinemaorder.entity.SysMovie;
import com.example.cinemaorder.entity.vo.SearchMovieInfoVo;
import com.example.cinemaorder.entity.vo.SearchMovieVo;
import com.example.cinemaorder.entity.vo.SysMovieVo;
import com.example.cinemaorder.entity.vo.SysRatingVo;
import com.example.cinemaorder.service.impl.SysMovieServiceImpl;
import com.example.cinemaorder.util.ResponseResult;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
public class SysMovieController{

    @Autowired
    private SysMovieServiceImpl sysMovieService;
    @Autowired
    RabbitTemplate rabbitTemplate;

//  电影详情页，查询影片相关信息
    @GetMapping("/sysMovie/find/{id}")
    public ResponseResult findMovieById(@PathVariable Long id) {
        return new ResponseResult(200,"查询电影信息成功",sysMovieService.findMovieById(id));
    }

//    电影详情页，查询商家相关信息
    @GetMapping("/sysMovie/hall")
    public Object hall(){
        return sysMovieService.getHalls();
    }

    //    查询所有的cinema信息
    @GetMapping("/sysMovie/cinema")
    public Object cinema(){
        return sysMovieService.getCinemas();
    }

    //    根据条件查询电影排片
    @PostMapping("/sysMovie/search")
    public Object search(@RequestBody SearchMovieInfoVo searchMovieInfoVo){
        // 动态sql查询所有符合条件的排片信息
        List<SearchMovieVo> search = sysMovieService.search(searchMovieInfoVo);
        // 修改时间展示格式
        search.stream().map(m ->
                {
                    m.setStartTime(m.getStartTime().substring(11, 16));
                    return m;
                })
                .collect(Collectors.toList());
        return search;
//        return rabbitTemplate.convertSendAndReceive("exchangetopicMovieSearch","movieSearch",searchMovieInfoVo);
    }

    // 查询出所有的电影id
    @GetMapping("/sysMovie/getIds")
    public Object getIds(){
        return sysMovieService.getIds();
    }

    //查询电影的平均评分
    @GetMapping("/sysMovie/rating/{movieId}/{userId}")
    public Object getRating(@PathVariable("movieId") Long movieId,@PathVariable("userId") Long userId){
        return new ResponseResult(200,"查询电影评分成功",sysMovieService.getRating(movieId,userId));
    }

    // 查询某个订单的评分
    @GetMapping("/sysMovie/getRatingByBill/{billId}")
    public Object getRatingByBill(@PathVariable("billId") Long billId){
        return new ResponseResult(200,"根据订单id查询电影评分成功",sysMovieService.getRatingByBill(billId));
    }

    //提交电影评分
    @PostMapping("/sysMovie/sendRating")
    public Object sendRating(@RequestBody SysRatingVo sysRatingVo){
        Boolean o = (Boolean) sysMovieService.sendRating(sysRatingVo);
        if(o == false){
            return new ResponseResult(400,"已经提交无需重复提交",null);
        }
        return new ResponseResult(200,"提交评分成功",null);
    }
}
